package cn.jc.pachong;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.List;

import cn.jc.pachong.dao.DataDAO;
import cn.jc.pachong.service.URLFinder;

public class pachongrukou {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		DataDAO.insert("http://www.mumayi.com");
		String starturl;
		while ((starturl = DataDAO.getUrl()) != null) {//还有没分析URL过的url
			String content = pachongrukou.getContent(starturl);
			List<String> urlList = URLFinder.findUrls(content);
			for (String url : urlList) {
				DataDAO.insert(url);
			}
		}
	}
	public static String getContent(String url) throws IOException{
		URL urll = new URL(url);
		Reader reader = new InputStreamReader(new BufferedInputStream(urll.openStream()));
		int c;
		StringBuilder sb = new StringBuilder();
		while ((c = reader.read()) != -1) {
			sb.append((char)c);
		}
		return sb.toString();
	}

}
